﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace WucCloud.Database
{
    /// <summary>
    /// 泛型仓储 接口
    /// </summary>
    public interface IBaseRepository<TEntity> where TEntity : class,new()
    {
        /// <summary>
        /// SqlSugarClient实体
        /// </summary>
        ISqlSugarClient Db { get; }

        #region Basic 基础方法
        /// <summary>
        /// 带过滤的查询
        /// </summary>
        /// <returns></returns>
        ISugarQueryable<TEntity> SugarQueryable();

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        IInsertable<TEntity> Insertable(TEntity entity);

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entities">实体对象集合</param>
        /// <returns></returns>
        IInsertable<TEntity> Insertable(List<TEntity> entities);

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        IUpdateable<TEntity> Updateable(TEntity entity);

        /// <summary>
        /// 批量编辑
        /// </summary>
        /// <param name="entities">实体对象集合</param>
        /// <returns></returns>
        IUpdateable<TEntity> Updateable(List<TEntity> entities);

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        IDeleteable<TEntity> Deleteable();
        #endregion

        #region Insert 新增方法
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>是否新增成功</returns>
        Task<bool> InsertAsync(TEntity entity);

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entities">实体对象集合</param>
        /// <returns>是否新增成功</returns>
        Task<bool> InsertAsync(List<TEntity> entities);
        #endregion

        #region Update 编辑方法
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>是否编辑成功</returns>
        Task<bool> UpdateAsync(TEntity entity);

        /// <summary>
        /// 批量编辑
        /// </summary>
        /// <param name="entities">实体对象集合</param>
        /// <returns>是否编辑成功</returns>
        Task<bool> UpdateAsync(List<TEntity> entities);
        #endregion

        #region Delete 删除方法

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyId">主键Id</param>
        /// <returns></returns>
        Task<bool> DeleteAsync(string keyId);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        Task<bool> DeleteAsync(TEntity entity);

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entities">实体对象集合</param>
        /// <returns></returns>
        Task<bool> DeleteAsync(TEntity[] entities);
        #endregion

        #region Query 查询方法

        /// <summary>
        /// 检验数据是否存在
        /// </summary>
        /// <param name="expression">Lambda表达式（查询条件）</param>
        /// <returns></returns>
        Task<bool> AnyAsync(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 检查信息总条数
        /// </summary>
        /// <param name="expression">Lambda表达式（查询条件）</param>
        /// <returns></returns>
        Task<int> CountAsync(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 通过Id查询
        /// </summary>
        /// <param name="keyId">主键Id</param>
        /// <returns></returns>
        Task<TEntity> FindByIdAsync(string keyId);

        /// <summary>
        /// 自定义条件查询（单条）
        /// </summary>
        /// <param name="expression">Lambda表达式（查询条件）</param>
        /// <returns></returns>
        Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        Task<List<TEntity>> QueryableAsync();

        /// <summary>
        /// 自定义条件查询（多条）
        /// </summary>
        /// <param name="expression">Lambda表达式（查询条件）</param>
        /// <returns></returns>
        Task<List<TEntity>> QueryableAsync(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        Task<List<TEntity>> QueryableAsync(PageModel page);

        /// <summary>
        /// 自定义条件分页查询
        /// </summary>
        /// <param name="expression">Lambda表达式（查询条件）</param>
        /// <param name="page">分页组件类</param>
        /// <returns></returns>
        Task<List<TEntity>> QueryableAsync(Expression<Func<TEntity, bool>> expression, PageModel page);
        #endregion
    }
}
